LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;

ENTITY fenpin IS
	PORT(
	clk:IN STD_LOGIC;
	--clear:IN STD_LOGIC;
	clk_out1:OUT STD_LOGIC;
	clk_out2:OUT STD_LOGIC;
	clk_out3:OUT STD_LOGIC;
	clk_out4:OUT STD_LOGIC;
	clk_1MHz,clk_4Hz:OUT STD_LOGIC;
	pulse_out:OUT STD_LOGIC
	);
END fenpin;

ARCHITECTURE a OF fenpin IS
	SIGNAL tmp1:INTEGER RANGE 0 TO 125999;
	SIGNAL tmp2:INTEGER RANGE 0 TO 99;
	signal tmp3:INTEGER RANGE 0 TO 1;
	signal tmp4:INTEGER RANGE 0 TO 49;
	SIGNAL tmp_1MHz:INTEGER RANGE 0 TO 24;
	SIGNAL tmp_4Hz:INTEGER RANGE 0 TO 24;
	
	--SIGNAL tmp1:INTEGER RANGE 0 TO 10;
	--SIGNAL tmp2:INTEGER RANGE 0 TO 10;
	SIGNAL clktmp1:STD_LOGIC;
	SIGNAL clktmp2:STD_LOGIC;
	SIGNAL clktmp3:STD_LOGIC;
	SIGNAL clktmp4:STD_LOGIC;
	SIGNAL clktmp_1MHz,clktmp_4Hz:STD_LOGIC;
	SIGNAL pulse_cnt1:INTEGER RANGE 0 TO 3871;
	SIGNAL pulse_cnt2:INTEGER RANGE 0 TO 3871;
	SIGNAL pulse_temp:STD_LOGIC;
	SIGNAL flag:STD_LOGIC;
BEGIN 
p1:PROCESS(clk)--200Hz
	BEGIN 
		IF clk'event AND clk='1' THEN
			IF tmp1=125999 THEN 
			--IF tmp1=9 THEN
				tmp1<=0;clktmp1<=NOT clktmp1;
			ELSE 
				tmp1<=tmp1+1;
			END IF;
		END IF;
	END PROCESS p1;
	
p2:	PROCESS(clktmp1)--1Hz
	BEGIN 
		IF clktmp1'event AND clktmp1='1' 	THEN
			IF tmp2=99 THEN
			--IF tmp2=9 THEN
				tmp2<=0;clktmp2<=NOT clktmp2;
			ELSE 
			tmp2<=tmp2+1;
			END IF;
		END IF;
	END PROCESS p2;
	
p3:PROCESS(clk)
	BEGIN 
		IF clk'event AND clk='1' THEN
			IF pulse_cnt1=3871	THEN
			--IF pulse_cnt1=10	THEN
				pulse_cnt1<=0;flag<='1';
			ELSE 
			pulse_cnt1<=pulse_cnt1+1;flag<='0';
			END IF;
		END IF;
	END PROCESS;
	


--p4:PROCESS(flag,clk)
	--BEGIN 
		--IF flag='1' THEN
			--IF pulse_cnt2=3871 THEN 
			--	IF pulse_cnt2=10 THEN 
				--pulse_cnt2<=0;
			--ELSE
				--pulse_cnt2<=pulse_cnt2+1;
			--END IF;
		--END IF;
	--END PROCESS;
	
--p5:PROCESS(clk)	
--BEGIN
	--IF pulse_cnt1<pulse_cnt2 THEN
	--pulse_temp<='0';
	--ELSE pulse_temp<='1';
	--END IF;
--END PROCESS;

p4:PROCESS(CLK)--for breathe led
	BEGIN 
		IF clk'event AND clk='1' THEN
			IF tmp3=1 THEN 
			--IF tmp1=9 THEN
				tmp3<=0;clktmp3<=NOT clktmp3;
			ELSE 
				tmp3<=tmp3+1;
			END IF;
		END IF;
	END PROCESS p4;
	
p5:	PROCESS(clktmp1)--2Hz for pattern changing
	BEGIN 
		IF clktmp1'event AND clktmp1='1' 	THEN
			IF tmp4=49 THEN
				tmp4<=0;clktmp4<=NOT clktmp4;
			ELSE 
			tmp4<=tmp4+1;
			END IF;
		END IF;
	END PROCESS p5;
	
p6:PROCESS(clk)--1MHz to divide frequncy for musical note 
BEGIN 
		IF clk'event AND clk='1' THEN
			IF tmp_1MHz=24 THEN 
				tmp_1MHz<=0;clktmp_1MHz<=NOT clktmp_1MHz;
			ELSE 
				tmp_1MHz<=tmp_1MHz+1;
			END IF;
		END IF;
	END PROCESS p6;
	
p7:	PROCESS(clktmp1)--4Hz for musical note changing
	BEGIN 
		IF clktmp1'event AND clktmp1='1' 	THEN
			IF tmp_4Hz=24 THEN
				tmp_4Hz<=0;clktmp_4Hz<=NOT clktmp_4Hz;
			ELSE 
			tmp_4Hz<=tmp_4Hz+1;
			END IF;
		END IF;
	END PROCESS p7;
	
	pulse_out<=pulse_temp;	
	clk_out1<=clktmp1;
	clk_out2<=clktmp2;
	clk_out3<=clktmp3;
	clk_out4<=clktmp4;
	clk_1MHz<=clktmp_1MHz;
	clk_4Hz<=clktmp_4Hz;
		END a;